import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 *is the control class of search book Gui.
 */
public class SearchBookCONTROL implements ListSelectionListener
{
	/** array of strings */
	public String[] searchData;
	/** array of strings */
	public String[] searchData2;
	/** array of strings */
	public String[] upDateData;
	/** array of strings */
	public String[] res;
	public DefaultListModel listModel;
	public DefaultListModel listModel1;
	public DefaultListModel listModel2;
	/** Indicate that a search was execute or not execute*/
	boolean seflag=false;
	public String value;  //  @jve:decl-index=0:
	public String value1;
	public String value2;
	public static JFrame details_frame;  //  @jve:decl-index=0:visual-constraint="10,34"
	public JPanel dpanel;
	public JList jBookDetailsList;
	public JLabel Title = null;
	public JLabel Author = null;
	public JLabel Summery = null;
	public JLabel Language = null;
	public JLabel Keywords = null;
	public JLabel Price = null;
	public JLabel Index = null;
	public JLabel Pdf = null;
	public JLabel Doc = null;
	public JLabel Fb2 = null;
	public JLabel Serial = null;
	public JLabel Serial1 = null;
	public JLabel numOS = null;
	public JScrollPane jScrollPane = null;
	public JLabel jLabelSerial = null;
	public JScrollPane jScrollLanguage = null;
	public JList jLanguageList = null;
	public JList jAuthorList = null;
	public JScrollPane jScrollAuthor = null;
	public String [] bdetails;
	
	
	public void  o_GetFields() 
	{
		String temp;
		searchData2 = new String[3];
		searchData2[0]=Main.gusers.id;		 
		searchData2[1]="YgetFields";			
		Main.log.connection.accept(searchData2[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");	
	}
	
	public void  o_GetSubjects() 
	{
		String temp;
		searchData2=new String[2];
		searchData2[0]=Main.gusers.id;
		searchData2[1]="getSubjects2";				
		Main.log.connection.accept("getSubjects2");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");		
			
	}
	/**
	 * this method turn of search book gui and turn on the main screen gui according to the user authorization 
	 * 
	 * @return void
	 */
	public void Back() 
	{
		Main.searchBookG.setEnabled(false);
		Main.searchBookG.setVisible(false);
		Main.bookManagement.BMflage=false;
		if(Main.gusers.authorozations==1) Main.log.frame.setSize(450,320);
		else if(Main.gusers.authorozations==2) Main.log.frame.setSize(450,400);
		else if(Main.gusers.authorozations==3) Main.log.frame.setSize(450,515);
		Main.welcomeScreen.panel.setEnabled(true);
		Main.welcomeScreen.panel.setVisible(true);
		
	}
	/**
	 *this method collect the search details and send it forward 
	 * @param choice
	 * 
	 * @return void
	 */
	public void Search(Integer choice) 
	{
		String temp = null;
		int i;
		searchData = new String[15];
		searchData[0]=Main.gusers.id;
		searchData[1]="searchbook";
		searchData[2]=Main.searchBookG.getJTitleField().getText();
		searchData[3]=Main.searchBookG.getJAuthorField().getText();	
		searchData[4]=Main.searchBookG.getJLanguageField().getText();	
		searchData[5]=Main.searchBookG.getJKeyWordsField().getText();	
		searchData[6]=Main.searchBookG.getJSummeryField().getText();
		searchData[7]=Main.searchBookG.getJIndexField().getText();
		if(Main.searchBookG.jCheckPdf.isSelected())
			searchData[8]="yes";
		else
			searchData[8]="";
		if(Main.searchBookG.jCheckDoc.isSelected())
			searchData[9]="yes";
		else
			searchData[9]="";
		if(Main.searchBookG.jCheckFb2.isSelected())
			searchData[10]="yes";
		else
			searchData[10]="";
		searchData[11]=Main.searchBookG.getJPriceField().getText();
		if (Main.searchBookG.SubjectCombo.getSelectedItem().equals(" "))
			searchData[12] = "";
		else
			searchData[12]=(String) Main.searchBookG.SubjectCombo.getSelectedItem();
		if (Main.searchBookG.FieldCombo.getSelectedItem().equals(" "))
			searchData[13] = "";
		else
			searchData[13]=(String) Main.searchBookG.FieldCombo.getSelectedItem();
		if (choice == 0)
			searchData[14]="or";
		else
			searchData[14]="and";
		
		if(searchData[2].equals("") && searchData[3].equals("") && searchData[4].equals("") &&
				searchData[5].equals("") && searchData[6].equals("") && searchData[7].equals("") && searchData[11].equals("") &&
				(!Main.searchBookG.jCheckFb2.isSelected()) && (!Main.searchBookG.jCheckPdf.isSelected()) && (!Main.searchBookG.jCheckDoc.isSelected()) &&
				searchData[12].equals("") && searchData[13].equals(""))
    	{
    		 JOptionPane.showMessageDialog(null,"All fields are empty, Please try again !","ERROR",JOptionPane.ERROR_MESSAGE);
    		 return;
    	}
		Main.log.connection.accept("search");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		if(temp.equals("error"))
    	{
    		 JOptionPane.showMessageDialog(null,"There is no such book in Catalog !","ERROR",JOptionPane.ERROR_MESSAGE);
    		 return;
    	}
		else
		{
			listModel=new DefaultListModel();
			for(i=0;i<this.res.length;i++)
				listModel.addElement(this.res[i]);
			if(Main.searchBookG.flag==0)
				Main.brc.lsearchUpdate(res);
			Main.searchBookG.SearchResult= new JList(listModel);
			Main.searchBookG.SearchResult.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			Main.searchBookG.jScrollPane.setViewportView(Main.searchBookG.SearchResult);
			Main.searchBookG.SearchResult.addListSelectionListener(this);
			Main.searchBookG.SearchResult.addMouseListener(new java.awt.event.MouseAdapter() 
			{
				public void mouseClicked(java.awt.event.MouseEvent e) 
				{
					String [] author;
					value2 = (String) Main.searchBookG.SearchResult.getSelectedValue();
					Main.welcomeScreen.d_Retrieve_Set(value2);
					author = Main.welcomeScreen.res1;
					listModel2=new DefaultListModel();
					for(int i=0;i<author.length;i++)
						listModel2.addElement(author[i]);
					Main.searchBookG.jAuthorList = new JList(listModel2);
					Main.searchBookG.jScrollResAuthor.setViewportView(Main.searchBookG.jAuthorList);
					Main.searchBookG.jAuthorList.setEnabled(false);
				}
			});
				
			Main.searchBookG.updateUI();
			seflag=true;
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0)
	{
		// TODO Auto-generated method stub
		
	}
	/**
	 *this method checks if search was executed if not return to search gui
	 *else set in variable value the details of the selected book from the book result list
	 *turn off search book control and gui then turn on update control activate d_Retrieve_Set method and turn on update gui and add it to the main frame
	 * 
	 * @param choice
	 * 
	 * @return void
	 */
	public void UpDate() 
	{
		if (seflag)
		{
			if(Main.searchBookG.SearchResult.getSelectedValue() == null)
	    	{
	    		 JOptionPane.showMessageDialog(null,"Please choose a book you want to update  !","ERROR",JOptionPane.ERROR_MESSAGE);
	    		 return;
	    	}
			value = (String) Main.searchBookG.SearchResult.getSelectedValue();
			Main.upDateBookC = new UpDateBookControl();
			Main.welcomeScreen.d_Retrieve_Set(value);
			Main.searchBookG.setEnabled(false);
			Main.searchBookG.setVisible(false);
			Main.upDateBookG = new UpDateBookGui();
			Main.log.frame.setSize(700, 430);
			Main.log.frame.add(Main.upDateBookG);
			Main.upDateBookG.setEnabled(true);
			Main.upDateBookG.setVisible(true);
		}
	}
	/**
	 *this method get the selected book from the book list in search book gui
	 *send it forward and if the result that returned from the data-base is ok
	 *the method will turn on book details frame
	 *
	 * @return void
	 */
	public void all_details() 
	{
		String [] bdetails=null;
		String [] language=null;
		String [] author=null;
		jLabelSerial = new JLabel();
		jLabelSerial.setText("JLabel");
		value = (String) Main.searchBookG.SearchResult.getSelectedValue();
		Main.welcomeScreen.d_Retrieve_Set(value);
		bdetails  = Main.welcomeScreen.res;
		details_frame = new JFrame("Book Details");
		details_frame.setPreferredSize(new Dimension(680, 450));
		details_frame.setSize(new Dimension(680, 450));
		dpanel = new JPanel();
		dpanel.setLayout(null);
		Serial = new JLabel();
		Serial.setBounds(new Rectangle(200, 120, 60, 20));
		Serial.setText("Serial :");
		Serial1 = new JLabel();
		Serial1.setBounds(new Rectangle(250, 120, 60,20));
		Serial1.setText(bdetails[0]);
		
		
		JLabel welcomeLabel = new JLabel("Book Detals");
		welcomeLabel.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,20));
		welcomeLabel.setBounds(230,20,300,50);
		listModel1=new DefaultListModel();
		for(int i=1;i<bdetails.length-1;i++)
			listModel1.addElement(bdetails[i]);
		jBookDetailsList = new JList(listModel1);
		jBookDetailsList.setEnabled(false);
		jScrollPane = new JScrollPane();
		jScrollPane.setBounds(new Rectangle(100, 150, 400, 220));
		jScrollPane.setViewportView(jBookDetailsList);
	
		language = Main.welcomeScreen.res2;
		listModel2=new DefaultListModel();
		for(int i=0;i<language.length;i++)
			listModel2.addElement(language[i]);
		jLanguageList=new JList(listModel2);
		jLanguageList.setEnabled(false);
		jScrollLanguage = new JScrollPane();
		jScrollLanguage.setBounds(510, 150, 140, 100);
		jScrollLanguage.setViewportView(jLanguageList);
		
		
		author = Main.welcomeScreen.res1;
		listModel2=new DefaultListModel();
		for(int i=0;i<author.length;i++)
			listModel2.addElement(author[i]);
		jAuthorList=new JList(listModel2);
		jAuthorList.setEnabled(false);
		jScrollAuthor = new JScrollPane();
		jScrollAuthor.setBounds(510, 270, 140, 100);
		jScrollAuthor.setViewportView(jAuthorList);
		
		
		Fb2 = new JLabel();
		Fb2.setBounds(new Rectangle(10, 263, 60, 21));
		Fb2.setText("FB2:");
		Doc = new JLabel();
		Doc.setBounds(new Rectangle(10, 243, 60, 21));
		Doc.setText("DOC:");
		Pdf = new JLabel();
		Pdf.setBounds(new Rectangle(10, 225, 60, 21));
		Pdf.setText("PDF:");
		
		numOS = new JLabel();
		numOS.setBounds(new Rectangle(10, 268, 100, 50));
		numOS.setText("Num of search:");
		Price = new JLabel();
		Price.setBounds(new Rectangle(10, 303, 60, 21));
		Price.setText("Price:");
		Index = new JLabel();
		Index.setBounds(new Rectangle(10, 205, 60, 21));
		Index.setText("Index:");
		Keywords = new JLabel();
		Keywords.setBounds(new Rectangle(10, 187, 65, 20));
		Keywords.setText("Keywords:");
		Language = new JLabel();
		Language.setBounds(new Rectangle(510, 120, 65, 19));
		Language.setText("Language:");
		Summery = new JLabel();
		Summery.setBounds(new Rectangle(10, 170, 65, 19));
		Summery.setText("Summery:");
		Author = new JLabel();
		Author.setBounds(new Rectangle(510, 253, 50, 19));
		Author.setText("Author:");
		Title = new JLabel();
		Title.setBounds(new Rectangle(10, 150, 50, 19));
		Title.setText("Title:");
		dpanel.add(welcomeLabel);
		dpanel.add(jScrollPane);
		dpanel.add(Title, null);
		dpanel.add(Author, null);
		dpanel.add(Summery, null);
		dpanel.add(Language, null);
		dpanel.add(Keywords, null);
		dpanel.add(Price, null);
		dpanel.add(Index, null);
		dpanel.add(Pdf, null);
		dpanel.add(Doc, null);
		dpanel.add(Fb2, null);
		dpanel.add(Serial, null);
		dpanel.add(Serial1, null);
		dpanel.add(jScrollAuthor,null);
		dpanel.add(jScrollLanguage,null);
		dpanel.add(numOS,null);
		dpanel.setEnabled(true);
		dpanel.setVisible(true);
		details_frame.add(dpanel);
       	details_frame.pack();
       	details_frame.setVisible(true);
		
	}
	/**
	 *this method turn on add new book gui and control and turn off search book gui and control
	 * 
	 * @return void
	 */
	public void add_Book() 
	{
		
		Main.addNBookC = new AddNewBookControl();
		Main.addNBookG = new AddNewBookGui();
		Main.log.frame.setSize(700, 560);
		Main.searchBookG.setEnabled(false);
		Main.searchBookG.setVisible(false);
		Main.log.frame.add(Main.addNBookG);
		Main.addNBookG.setEnabled(true);
		Main.addNBookG.setVisible(true);
	}

///////////////////////////////Gil///////////////////////////////////////////////////////
	public void addCart(Boolean[] formats,String serial,String lan)
	{
		String temp;
		searchData = new String[6];
		searchData[0]=Main.gusers.id;
		searchData[1]="addtocart";
		searchData[2]="";
		if(formats[0])
			searchData[2]+="pdf ";
		if(formats[1])
			searchData[2]+="doc ";
		if(formats[2])
			searchData[2]+="fb2";
	    searchData[3]=lan;
	    searchData[4]=serial;
	    searchData[5]=Main.gusers.id;
	 	Main.log.connection.accept("search");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.equals("OK"))
			JOptionPane.showMessageDialog(null,"Book Added Succesfully to Cart");
		else if(temp.equals("CART"))
			JOptionPane.showMessageDialog(null,"You Have This Book In Your Cart Already","ERROR",JOptionPane.ERROR_MESSAGE);
			else if(temp.equals("PURCHASED"))
				JOptionPane.showMessageDialog(null,"You Already Purchased This Book","ERROR",JOptionPane.ERROR_MESSAGE);
			else if(temp.equals("WRONG"))
				JOptionPane.showMessageDialog(null,"Error in Server. Contact System Administrator","ERROR",JOptionPane.ERROR_MESSAGE);
				
	}
///////////////////////////////Gil///////////////////////////////////////////////////////
	
}
